package net.wofly.sale.order.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.wofly.common.persistence.PageWrapper;
import net.wofly.common.web.BaseController;
import net.wofly.finance.expense.domain.ExpenseAccount;
import net.wofly.framework.util.ReturnBean;
import net.wofly.sale.order.domain.ReturnSales;
import net.wofly.sale.order.service.IReturnSalesFlowService;
import net.wofly.sale.order.service.IReturnSalesService;
import net.wofly.sale.order.validator.OrderValidator;
import net.wofly.sale.stock.validator.ResourceAskValidator;

@Controller
@RequestMapping(value = "/api/returnSales")
@Api(description = "退货接口(商贸模块)")
public class ReturnSalesController extends BaseController {
    
    private static final String DEFAULT_PAGE_NUM = "1";
    private static final String DEFAULT_PAGE_SIZE = "10";
    Logger logger = LoggerFactory.getLogger(ReturnSalesController.class);
    
    @Autowired
    IReturnSalesService iReturnSalesService;
    @Autowired
    IReturnSalesFlowService iReturnSalesFlowService ;
    
    @ApiOperation(value = "退货-根据订单ID查询可退货内容")
    @RequestMapping(value = "/getBackGoodsForReturnSales", method = {RequestMethod.POST})
    @ApiImplicitParams({@ApiImplicitParam(name = "orderID", value = "订单ID", required = true, dataType = "String",
            paramType = "form")})
    @ResponseBody
    public ReturnBean<ReturnSales> getBackGoodsForReturnSales(String orderID) {
        ReturnBean<ReturnSales> returnBean;
        String errorMessage = OrderValidator.validateOrderID(orderID);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<>(-1, errorMessage);
        } else {
            returnBean = iReturnSalesService.getBackGoodsForReturnSales(orderID);
        }
        return returnBean;
    }
    
    @ApiOperation(value = "退货-预计算退货商品及赠品")
    @RequestMapping(value = "/preCalcReturnSales", method = {RequestMethod.POST})
    @ApiImplicitParams({@ApiImplicitParam(name = "orderID", value = "订单ID", required = true, dataType = "String",
            paramType = "path"),
            @ApiImplicitParam(name = "returnSales", value = "退货对象", required = true, dataType =
                    "ReturnSales", paramType = "body")})
    @ResponseBody
    public ReturnBean<ReturnSales> preCalcReturnSales(String orderID, @RequestBody ReturnSales returnSales) {
        ReturnBean<ReturnSales> returnBean;
        String errorMessage = OrderValidator.validateReturnSalesParams(orderID, returnSales);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<>(-1, errorMessage);
        } else {
            returnBean = iReturnSalesService.preCalcReturnSales(orderID, returnSales);
        }
        return returnBean;
    }
    
    @ApiOperation(value = "退货-保存退货申请")
    @RequestMapping(value = "/saveReturnSales", method = {RequestMethod.POST})
    @ApiImplicitParams({@ApiImplicitParam(name = "orderID", value = "订单ID", required = true, dataType = "String",
            paramType = "path"),
            @ApiImplicitParam(name = "returnSales", value = "退货对象", required = true, dataType =
                    "ReturnSales", paramType = "body")})
    @ResponseBody
    public ReturnBean<ReturnSales> saveReturnSales(String orderID, @RequestBody ReturnSales returnSales) {
        ReturnBean<ReturnSales> returnBean;
        String errorMessage = OrderValidator.validateReturnSalesParams(orderID, returnSales);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<>(-1, errorMessage);
        } else {
            returnBean = iReturnSalesService.saveReturnSales(orderID, returnSales);
        }
        return returnBean;
    }
    
    /**
     * 退货详情
     *
     * @param returnSalesID
     * @return
     */
    @ApiOperation(value = "退货-根据退货单ID查询退货详情")
    @RequestMapping(value = "/getReturnSales", method = {RequestMethod.POST})
    @ApiImplicitParams({@ApiImplicitParam(name = "returnSalesID", value = "退货单ID", required = true, dataType = "String",
            paramType = "form")})
    @ResponseBody
    public ReturnBean<ReturnSales> getReturnSales(String returnSalesID) {
        
        ReturnBean<ReturnSales> returnBean;
        String errorMessage = OrderValidator.validateReturnSalesID(returnSalesID);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<>(-1, errorMessage);
        } else {
            returnBean = iReturnSalesService.getReturnSales(returnSalesID);
        }
        return returnBean;
    }
    
    @ApiOperation(value = "退货-编辑并保存退货申请")
    @RequestMapping(value = "/editAndSaveReturnSales", method = {RequestMethod.POST})
    @ApiImplicitParams({@ApiImplicitParam(name = "returnSales", value = "退货对象", required = true, dataType =
            "ReturnSales", paramType = "body")})
    @ResponseBody
    public ReturnBean<ReturnSales> editAndSaveReturnSales(@RequestBody ReturnSales returnSales) {
        ReturnBean<ReturnSales> returnBean;
        String errorMessage = OrderValidator.validateEditAndReturnSalesParams(returnSales);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<>(-1, errorMessage);
        } else {
            returnBean = iReturnSalesService.editAndSaveReturnSales(returnSales);
        }
        return returnBean;
    }
    
    /**
     * 删除退货
     *
     * @param returnSalesID
     * @return
     */
    @ApiOperation(value = "退货-删除退货申请")
    @RequestMapping(value = "/deleteReturnSales", method = {RequestMethod.POST})
    @ApiImplicitParams({@ApiImplicitParam(name = "returnSalesID", value = "退货单ID", required = true, dataType = "String",
            paramType = "form")})
    @ResponseBody
    public ReturnBean<ReturnSales> deleteReturnSales(String returnSalesID) {
        
        ReturnBean<ReturnSales> returnBean;
        String errorMessage = OrderValidator.validateReturnSalesID(returnSalesID);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<>(-1, errorMessage);
        } else {
            returnBean = iReturnSalesService.deleteReturnSales(returnSalesID);
        }
        return returnBean;
    }
    
    /**
     * 删除退货
     *
     * @param returnSalesID
     * @return
     */
    @ApiOperation(value = "退货-取消退货申请")
    @RequestMapping(value = "/cancelReturnSales", method = {RequestMethod.POST})
    @ApiImplicitParams({@ApiImplicitParam(name = "returnSalesID", value = "退货单ID", required = true, dataType = "String",
            paramType = "form")})
    @ResponseBody
    public ReturnBean<ReturnSales> cancelReturnSales(String returnSalesID) {
        
        ReturnBean<ReturnSales> returnBean;
        String errorMessage = OrderValidator.validateReturnSalesID(returnSalesID);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<>(-1, errorMessage);
        } else {
            returnBean = iReturnSalesService.cancelReturnSales(returnSalesID);
        }
        return returnBean;
    }
    
    /**
     * 退货列表查询
     *
     * @param returnSalesNo
     * @param returner
     * @param status
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "退货-查询退货申请单列表")
    @RequestMapping(value = "/getReturnSalesByCnd", method = {RequestMethod.POST})
    @ApiImplicitParams({@ApiImplicitParam(name = "returner", value = "退货人（下退货订单的人）", dataType = "String", paramType =
            "form"),
            @ApiImplicitParam(name = "customerName", value = "客户", dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "returnSalesNo", value = "退货单编号", dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "status", value = "退货单状态", dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", dataType = "Long", paramType = "form"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", dataType = "Long", paramType = "form"),
            @ApiImplicitParam(name = "pageNum", value = "页码（默认第一页）", required = true, dataType = "int", paramType =
                    "form"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数（默认10条）", required = true, dataType = "int",
                    paramType = "form")})
    @ResponseBody
    ReturnBean<PageWrapper<ReturnSales>> getReturnSalesByCnd(
            String returner, String customerName, String returnSalesNo, Integer status, Long startTime, Long endTime,
            @RequestParam(defaultValue = DEFAULT_PAGE_NUM) Integer pageNum,
            @RequestParam(defaultValue = DEFAULT_PAGE_SIZE) Integer pageSize) {
        ReturnBean<PageWrapper<ReturnSales>> returnBean;
        String errorMessage = "";
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<>(-1, errorMessage);
        } else {
            returnBean = iReturnSalesService.getReturnSalesByCnd(returner, returnSalesNo, customerName, status,
                    startTime,
                    endTime, pageNum, pageSize);
        }
        return returnBean;
    }
    
    @ApiOperation(value = "发起退货单申请工作流", notes = "发起退货单申请工作流")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "申请人ID", required = true, dataType = "String", paramType = "form") ,
    						@ApiImplicitParam(name = "returnSalesID", value = "退货单ID", required = true, dataType = "String", paramType = "form")
    		})
    @RequestMapping(value = "/startWorkflow", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<String> startWorkflow(HttpServletRequest request ,String userId ,String returnSalesID) {
    		Map<String, Object> variables = new HashMap<>() ;
    		
    		String errorMessage = ResourceAskValidator.validateStartWF(userId,returnSalesID);
    		if (StringUtils.isNotEmpty(errorMessage)) {
                return new ReturnBean<>(1, errorMessage);
    		}
    		
		return iReturnSalesFlowService.startWorkflow(userId ,returnSalesID, variables) ;
    }
	
    @ApiOperation(value = "获取退货单申请待办", notes = "获取退货单申请待办")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "申请人ID", required = true, dataType = "String", paramType = "form")
    		})
    @RequestMapping(value = "/findToDoList", method = {RequestMethod.POST})
    @ResponseBody
	public ReturnBean<List<ReturnSales>> findToDoList(String userId) {
		
		return new ReturnBean<>(ReturnBean.CODE_SUCCESS , "获取退货单申请待办成功" ,iReturnSalesFlowService.findToDoList(userId)) ;
	}
	
    @ApiOperation(value = "完成退货单申请工作流任务", notes = "完成退货单申请工作流任务")
    @ApiImplicitParams({@ApiImplicitParam(name = "userID", value = "申请人ID", required = true, dataType = "String", paramType = "form"),
    						@ApiImplicitParam(name = "taskID", value = "任务ID", required = true, dataType = "String", paramType = "form")
    		})
    @RequestMapping(value = "/complete", method = {RequestMethod.POST})
    @ResponseBody
	public ReturnBean<String> complete(HttpServletRequest request ,String userID ,String taskID) {
    	
		Map<String, String[]> mapRequest = request.getParameterMap() ;
		
		return iReturnSalesFlowService.complete(userID, taskID, mapRequest) ;
	}
	
    @ApiOperation(value = "为工作流获取退货单申请详情", notes = "为工作流获退货单申请详情")
    @ApiImplicitParams({@ApiImplicitParam(name = "returnSalesID", value = "退货单申请ID", required = true, dataType = "String", paramType = "form")
    		})
    @RequestMapping(value = "/getReturnSalesForFlow", method = {RequestMethod.POST})
    @ResponseBody
	public ReturnBean<ReturnSales> getReturnSalesForFlow(String returnSalesID) {
		return iReturnSalesFlowService.getReturnSalesForFlow(returnSalesID);
	}
    
}
